<!--
 * @Author: [dengyih]
 * @Date: 2021-10-24 14:52:43
 * @LastEditors: [dengyih]
 * @LastEditTime: 2021-10-24 19:43:31
 * @Description: 
-->
<template>
  <div>
    <!-- 第一行开始 -->
    <div><el-button type="primary" @click="toAdd">新增</el-button> <el-button type="primary" @click="dialogVisible = true">预览</el-button></div>
    <!-- 第一行结束 -->
    <!-- 表格开始 -->
    <div>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" :index="1" label="序号" align="center"> </el-table-column>
        <el-table-column prop="name" label="轮播图名称" align="center"> </el-table-column>
        <el-table-column prop="introduce" label="轮播图描述" align="center"> </el-table-column>
        <el-table-column prop="url" label="轮播图图片" align="center">
          <template slot-scope="scope">
            <div>
              <el-popover placement="right" trigger="hover">
                <el-image slot="reference" style="width: 25px; height: 25px" :src="scope.row.url"></el-image>
                <el-image style="width:200px; height: 200px" :src="scope.row.url"></el-image>
              </el-popover>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" align="center">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.status === '正常'">正常</el-tag>
            <el-tag type="danger" v-else>停用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="toEdit(scope.row)" style="color:green">编辑</el-button>
            <el-button type="text" size="small" @click="toDelete(scope.row.id)" style="color:red">删除</el-button>
            <el-button type="text" size="small" v-if="scope.row.status === '正常'" @click="toUse(scope.row, '停用')">停用</el-button>
            <el-button type="text" size="small" v-else @click="toUse(scope.row, '正常')">启用</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 表格结束 -->
    <!-- 模态框开始 -->
    <el-dialog :title="title" :visible.sync="visible">
      <el-form :model="form" :rules="rules" ref="myForm">
        <el-form-item label="名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="轮播图片" :label-width="formLabelWidth" prop="url">
          <el-upload class="avatar-uploader" :action="uploadFileURL" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="form.url" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="描述" :label-width="formLabelWidth" prop="introduce">
          <el-input v-model="form.introduce" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="toSubmit('myForm')">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 模态框结束 -->
    <!-- 预览框开始 -->
    <el-dialog title="大屏预览" :visible.sync="dialogVisible" width="80%" :before-close="handleClose">
      <div class="block">
        <el-carousel height="350px">
          <el-carousel-item v-for="item in tableData" :key="item.id">
            <img :src="item.url" style="width:100%;height:100%" />
          </el-carousel-item>
        </el-carousel>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogVisible = false">关 闭</el-button>
      </span>
    </el-dialog>
    <!-- 预览框结束 -->
  </div>
</template>

<script>
import { showFileURL, uploadFileURL } from '@/utils/config'
import { findAll, deleteById, saveOrUpdate } from '@/api/carousel'
export default {
  data() {
    return {
      // 预览框
      dialogVisible: false,
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        url: [{ required: true, message: '请上传轮播图片', trigger: 'blur' }]
      },
      formLabelWidth: '80px',
      imageUrl: '',
      // 图片显示的基础路径
      showFileURL,
      // 文件上传得文件
      uploadFileURL,
      visible: false,
      tableData: [],
      form: {},
      title: ''
    }
  },
  created() {
    this.pageQuery()
  },
  methods: {
    // 预览

    //停用 启用
    async toUse(row, e) {
      let temp = {
        ...row,
        status: e
      }
      await saveOrUpdate(temp)
      this.pageQuery()
    },
    //模态框 确认
    toSubmit(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          // 校验通过
          let res = await saveOrUpdate(this.form)
          this.visible = false
          // console.log(res)
          this.$message({
            message: res.message,
            type: 'success'
          })
          // 刷新页面
          this.pageQuery()
        } else {
          this.$message.error('保存失败')
          return false
        }
      })
    },
    //添加
    toAdd() {
      // 显示模态框
      this.visible = true
      this.title = '添加栏目信息'
      this.form = {}
      this.imageUrl = ''
    },
    //编辑
    toEdit(row) {
      this.visible = true
      this.title = '修改栏目信息'
      this.form = { ...row }
      this.imageUrl = this.form.url
    },
    //渲染表格内容
    async pageQuery() {
      let res = await findAll()
      this.tableData = res.data
    },
    // 删除轮播图
    toDelete(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          // 确定删除 发送删除请求
          let res = await deleteById({ id })
          this.pageQuery()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          // 点击取消 提示用户取消删除
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    //上传图片
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      console.log(res)
      // 展示图片 图片基础路径+图片地址
      this.form.url = showFileURL + res.data.id
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    }
  }
}
</script>

<style></style>
